#!/usr/bin/env python
# -*- coding: utf-8 -*-
# 

import sys,os
from ConfigParser import ConfigParser

def loadData(filelist, configure) :
    table = configure['table']
    AccessorModule = __import__(configure['accessor'])
    accessor = AccessorModule.Accessor(configure)

    filelist_fp = open(filelist)
    for filename in filelist_fp.xreadlines() :
        filename = filename.strip()
        if not filename : continue
        fp = open(filename) 
        for line in fp.xreadlines() :
            line = line.strip()
            if not line : continue
            ok = accessor.insert(table, line)
            if not ok :
                raise RuntimeError
        fp.close()
    return  True
    ## loadData()

def verifyData(filelist, configure) :
    table = configure['table']
    AccessorModule = __import__(configure['accessor'])
    accessor = AccessorModule.Accessor(configure)

    filelist_fp = open(filelist)
    for filename in filelist_fp.xreadlines() :
        filename = filename.strip()
        if not filename : continue
        fp = open(filename) 
        for line in fp.xreadlines() :
            line = line.strip()
            if not line : continue
            ok = accessor.verify(table, line)
            if not ok :
                raise RuntimeError,'Fail to verify (table:%s) "%s"' % (table, line)
        fp.close()
    return  True
    ## verifyData()

def deleteData(filelist, configure) :
    table = configure['table']
    AccessorModule = __import__(configure['accessor'])
    accessor = AccessorModule.Accessor(configure)

    filelist_fp = open(filelist)
    for filename in filelist_fp.xreadlines() :
        filename = filename.strip()
        if not filename : continue
        fp = open(filename) 
        for line in fp.xreadlines() :
            line = line.strip()
            if not line : continue
            ok = accessor.erase(table, line)
            if not ok :
                raise RuntimeError
        fp.close()
    return  True
    ## deleteData()


def parseConfigure(config_file) :
    configure = parseSlb64Configure(config_file)

    return configure
    ## parseConfigure()

def parseSlb64Configure(config_file) :
    ini = ConfigParser()
    ini.optionxform = lambda x: x
    ini.read(config_file)

    SECTION = 'slb64'
    if not ini.has_section(SECTION) :
        raise RuntimeError, 'Section [%s] does NOT exist.' % SECTION

    configure = {}

    OPTION = 'table'
    if not ini.has_option(SECTION, OPTION) :
        raise RuntimeError, 'Section [%s] has no option "%s".' % (SECTION, OPTION)
    configure['table'] = ini.get(SECTION, OPTION)

    OPTION = 'datadir'
    if not ini.has_option(SECTION, OPTION) :
        raise RuntimeError, 'Section [%s] has no option "%s".' % (SECTION, OPTION)
    configure['datadir'] = ini.get(SECTION, OPTION)

    OPTION = 'accessor'
    if not ini.has_option(SECTION, OPTION) :
        raise RuntimeError, 'Section [%s] has no option "%s".' % (SECTION, OPTION)
    configure['accessor'] = ini.get(SECTION, OPTION)

    OPTION = 'progress'
    if not ini.has_option(SECTION, OPTION) :
        configure['progress'] = 0
    else :
        configure['progress'] = ini.getint(SECTION, OPTION)

    return configure
    ## parseSlb64Configure()


if __name__ == '__main__' :

    from optparse import OptionParser
    parser = OptionParser()
    parser.add_option("-c", "--config", dest="config", type="string",
                        help='path to config file')
    parser.add_option("-l", "--filelist", dest="filelist", type="string",
                        help='path to filelist')

    parser.add_option("-v", "--verbose", dest="verbose", action="store_true", default=False,
                        help='show detail information')

    (options, args) = parser.parse_args()
    if options.filelist is None :
        parser.print_help( sys.stdout )
        sys.exit(0)

    config = parseConfigure(options.config)
    ##loadData(options.filelist, config)

    verifyData(options.filelist, config)

    deleteData(options.filelist, config)

    sys.exit(0)

